package com.erp.module.oem.XxOem

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.OemBean
import com.erp.bean.PhotoBean
import com.erp.module.oem.OemContract
import com.erp.module.oem.OemModel
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject
import java.util.ArrayList

class IXxOem (val KView: OemContract.XxOemView, val context: Context, var oembean: OemBean, val id: String) : OemContract.IXxOem {



    var md = OemModel()
    override fun getOemPhoto() {
        md.getOemPhoto(context,id,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                json(data)
                KView.getOemPhotoOk()
            }

            override fun onNot(throwable: String?) {
                KView.getOemPhotoNo()
            }

        })
    }

    override fun postOemPhoto(bean: OemBean.gongxuBean) {
        if(bean.workshop_section_name == ""){
            Myutil.MyTost("工序名称是必须的")
            KView.postOemPhotoNo()
            return
        }
        if(bean.oem_ws_id == "") {
            KView.showdialog("正在新建工序")
        }else{
            KView.showdialog("正在修改工序")
        }
        md.postDuizhangPhoto(context,id,bean,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                json(data)
                KView.postOemPhotoOk()
            }

            override fun onNot(throwable: String?) {
                KView.postOemPhotoNo()
            }

        })
    }

    override fun deleteOemPhoto(ws_id: String) {
        KView.showdialog("正在删除工序")
        md.deleteOemPhoto(context,ws_id,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                KView.deleteOemPhotoOk()
            }

            override fun onNot(throwable: String?) {
                KView.deleteOemPhotoNo()
            }

        })
    }

    override fun postWangong(count: String) {
        md.wangongOemPhoto(context,id,count,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                KView.postWangongOk()
            }

            override fun onNot(throwable: String?) {
                KView.postWangongNo()
            }

        })
    }


    fun json(data:String?){
        if (data==null){
            return
        }
        try {
            val j = JSONObject(data)
            val jo = JsonUtil.getjovolue(j,"data")
            oembean.accomplish_count = JsonUtil.getvolue(jo, "accomplish_count")
            oembean.id = JsonUtil.getvolue(jo, "id")
            oembean.o_onum = JsonUtil.getvolue(jo, "o_onum")
            oembean.shop_name = JsonUtil.getvolue(jo, "shop_name")
            oembean.gongxulist.clear()
            val js = JsonUtil.getjsvolue(jo,"photos")
            for (i in 0 until js.length()) {
                val b = OemBean.gongxuBean()
                val joo = js.get(i) as JSONObject
                b.oem_ws_id = JsonUtil.getvolue(joo, "oem_ws_id")
                b.workshop_section_name = JsonUtil.getvolue(joo, "workshop_section_name")
                val jsphoto = JsonUtil.getjsvolue(joo,"photos")
                for (j in 0 until jsphoto.length()){
                    val jophoto = jsphoto.get(j) as JSONObject
                    val photo = PhotoBean()
                    photo.fid = JsonUtil.getvolue(jophoto, "fid")
                    photo.uri = JsonUtil.getvolue(jophoto, "uri")
                    b.listphoto.add(photo)
                }
                oembean.gongxulist.add(b)
            }
            oembean.gongxulist.add(OemBean.gongxuBean())
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }
}